int openVTKFile( string fileName, string colorArrayName )
{
	int _retCode = 0;
	int * labelofcolor;
	try
	{
		vtkDataArray		*_colorArray;
	
		// read the file
		reader = vtkPolyDataReader::New();
		reader->SetFileName(fileName.c_str());
		reader->OpenVTKFile();

		reader->Update();

		// store poly data pointer
		polyData = reader->GetOutput();
		// build links
		polyData->BuildLinks();

		//ptCnt = _colorArray->GetNumberOfTuples();
		ptCnt = 0;
		vtkCellArray *cells = polyData->GetPolys();
		vtkIdType npts, *ppts = 0;
		cells->InitTraversal();
		while(cells->GetNextCell(npts, ppts)) {
			for(int i = 0; i < npts; i++) {
				if(ppts[i] > ptCnt) { ptCnt = ppts[i]; }
			}
		}
		ptCnt += 1;
		cellCnt = polyData->GetNumberOfCells();
		cout << ptCnt << " pts " << cellCnt << " cells" << endl;

		// locate color array
		if(0 == (_colorArray = polyData->GetPointData()->GetArray(colorArrayName.c_str())))
		{
			colorinfo = false;
			throw 0;
		}
		
	}
	catch ( int &_e )
	{
		_retCode = 100+_e;
		cout << "Error code " << _e << endl;
	}
	return _retCode;
}



void WriteVTK(string filename, vector<long> color) {

	ofstream ofs;
	ofs.open(filename.c_str());
	ofs << "# vtk DataFile Version 3.0" << endl;
	ofs	<< "vtk output" << endl;
	ofs	<< "ASCII" << endl;
	ofs	<< "DATASET POLYDATA" << endl;

	int numofpts = seg->getPtCnt();
	ofs	<< "POINTS " << numofpts  << " float" << endl;
	for(int i = 0; i < numofpts; i++) {
		double pt[3];
		seg->getPtCorr(i, pt);
		ofs << pt[0] << ' ' << pt[1] << ' ' << pt[2] << endl;
	}

	int numofcells = seg->getCellCnt();
	ofs << "POLYGONS " << numofcells << ' ' << numofcells*4 << endl;
	for(int i = 0; i < numofcells; i++) {
		vtkIdType npts, *ppts = 0;
		seg->polyData->GetCellPoints((vtkIdType)i, npts, ppts);
		ofs << "3 " << ppts[0] << ' ' << ppts[1] << ' ' << ppts[2] << endl;
	}

	ofs << "POINT_DATA " << numofpts << endl;
	ofs << "VECTORS color unsigned_char" << endl;
	for(int i = 0; i < numofpts; i++) {
		
		int rgb[3];
		color2rgb(color[i], rgb);
		ofs << rgb[0] << ' ' << rgb[1] << ' ' << rgb[2] << endl;
		
	}
	ofs.close();
	cout << "file written" << endl;

}